package com.itheima.dao;

import com.itheima.domain.Student;

public class StudentDao implements BaseStudentDao {
    //获取数组
    private static Student[] stus= new Student[80];
    static {
        Student wu = new Student("0001", "吴亦凡", 31, "1991-01-01");
        Student lu = new Student("0002", "鹿晗", 31, "1991-01-01");
        Student huang = new Student("0003", "黄子韬", 31, "1991-01-01");
        Student zhang = new Student("0004", "张艺兴", 31, "1991-01-01");
        stus[0]=wu;
        stus[1]=lu;
        stus[2]=huang;
        stus[3]=zhang;

    }

    @Override
    public boolean addStudent(Student student) {
        //将学生添加到数组中
        //将学生放在数组中的空位上
        for (int i = 0; i < stus.length; i++) {
            //stus[i]
            Student stu= stus[i];
            if(stu==null){
                stus[i]=student;
                return true;
            }
        }
        //给出结果
        return false;

    }
    //查询所有学生的方法
    public Student[] findAllStudent() {
        return stus;
    }

    //删除方法
    //参数  String sid
    //返回值   没有
    public void  deleteStudent(String sid){
        //找到sid 所在的位置
        int index = getIndex(sid);
        //找到直接赋值为null
        stus[index]=null;

    }
    //根据学号找索引  查
    //参数 String sid
    //返回值 int
    public int getIndex(String sid){
        //遍历数组
        for (int i = 0; i < stus.length; i++) {
            //获取每一个学生
            //判断学生的sid 和参数sid 是否一致
            Student stu = StudentDao.stus[i];
            if(stu!=null&&stu.getSid().equals(sid)){
                //一致 返回索引
                return i;
            }
            //不一致 下一次
        }
        //所有的都找不到
        return -1;

    }

    public void updateStudent(String sid, Student student) {
        //根据学号找索引
        int index = getIndex(sid);
        //替换
        stus[index]=student;
    }
}
